package com.xmtlxx.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.oms.domain.BoxDept;
import com.xmtlxx.oms.domain.bo.BoxDeptBo;
import com.xmtlxx.oms.domain.vo.BoxDeptVo;
import com.xmtlxx.oms.mapper.BoxDeptMapper;
import com.xmtlxx.oms.service.IBoxDeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 箱子领用Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-12-07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BoxDeptServiceImpl implements IBoxDeptService {

    private final BoxDeptMapper baseMapper;

    /**
     * 查询箱子领用
     *
     * @param id 箱子领用主键
     * @return 箱子领用
     */
    @Override
    public BoxDeptVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectBoxDeptVoByID(id);
    }

    /**
     * 查询箱子领用列表
     *
     * @param bo 箱子领用
     * @return 箱子领用
     */
    @Override
    public TableDataInfo<BoxDeptVo> queryPageList(BoxDeptBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<BoxDept> lqw = buildQueryWrapper(bo);
       // Page<BoxDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<BoxDeptVo> result = baseMapper.selectPageBoxDeptList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询箱子领用列表
     *
     * @param bo 箱子领用
     * @return 箱子领用
     */
    @Override
    public List<BoxDeptVo> queryList(BoxDeptBo bo) {
       // LambdaQueryWrapper<BoxDept> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectBoxDeptList(bo);
    }

    private LambdaQueryWrapper<BoxDept> buildQueryWrapper(BoxDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BoxDept> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBoxId()), BoxDept::getBoxId, bo.getBoxId());
        lqw.eq(StringUtils.isNotBlank(bo.getBoxNo()), BoxDept::getBoxNo, bo.getBoxNo());
        lqw.eq(bo.getBoxDept() != null, BoxDept::getBoxDept, bo.getBoxDept());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BoxDept::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), BoxDept::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增箱子领用
     *
     * @param bo 箱子领用
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(BoxDeptBo bo) {
        BoxDept add = BeanUtil.toBean(bo, BoxDept.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改箱子领用
     *
     * @param bo 箱子领用
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(BoxDeptBo bo) {
        BoxDept update = BeanUtil.toBean(bo, BoxDept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BoxDept entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除箱子领用
     *
     * @param ids 需要删除的箱子领用主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
